package com.PictureSite.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PictureSite.DatabaseAccess.PictureSiteCreatePostDAO;
import com.PictureSite.model.PictureSitePostModel;
import com.PictureSite.model.UserPost;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.files.FileService;

public class PictureSiteHomeController extends PictureSiteController {
	
	private final FileService fileService;
	
	public PictureSiteHomeController(final FileService fileService) {
		this.fileService = fileService;
	}
	
	private PictureSiteCreatePostDAO dataStore = new PictureSiteCreatePostDAO(); 
	
	public void filldata(PictureSitePostModel model,HttpServletRequest req, HttpServletResponse resp) throws IOException {

		List<UserPost> userPosts = new ArrayList<UserPost>();
		
		List<Entity> tempList = dataStore.getPostByDateDesc(model.getPageLimit(),model.getPageNum());
		
		for (Entity row : tempList) {
			
			UserPost userpost = new UserPost();
			userpost.setTitle(row.getProperty("title").toString());
			userpost.setUserName(row.getProperty("userName").toString());
			userpost.setDatePosted(row.getProperty("date").toString());
			userpost.setBlobId(row.getProperty("blobId").toString());
			userPosts.add(userpost);
		}
		
		model.setUserPosts(userPosts);
		
	}
	
	public int getPageNum(HttpServletRequest req) throws IOException {
		//Need to remake and have servlet based parameter extraction.
		String pageNumberStr = req.getPathInfo().replace("/", "");
		int pageNumber = Integer.parseInt(pageNumberStr);
		return pageNumber;
	}
	
}
